import Controller.LoginStageController;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.xml.stream.events.EndElement;
import java.io.IOException;
import java.net.URL;

/**
 * @program: StudentMS
 * @description: main application
 * @author: XuYuanjun
 * @create: 2020-03-07 13:13
 **/
public class MainApp extends Application {

    private double xOffSet = 0;
    private double yOffSet = 0;

    /**
     * The main entry point for all JavaFX applications.
     * The start method is called after the init method has returned,
     * and after the system is ready for the application to begin running.
     *
     * <p>
     * NOTE: This method is called on the JavaFX Application Thread.
     * </p>
     *
     * @param primaryStage the primary stage for this application, onto which
     *                     the application scene can be set. The primary stage will be embedded in
     *                     the browser if the application was launched as an applet.
     *                     Applications may create other stages, if needed, but they will not be
     *                     primary stages and will not be embedded in the browser.
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            initLoginStage(primaryStage);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    // 初始化登录界面
    private void initLoginStage(Stage stage) throws IOException {
        try {
            stage.setTitle("学生管理系统");
            stage.setResizable(false);

            // 隐藏标题栏
            // primaryStage.initStyle(StageStyle.UNDECORATED);
            stage.initStyle(StageStyle.TRANSPARENT);

            // 设置 stage 的图标
            stage.getIcons().add(new Image("Resoues/stage.png"));
            // 加载fxml（界面）
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("View/LoginStage.fxml"));
            AnchorPane pane = fxmlLoader.load();
            //AnchorPane pane = FXMLLoader.load(getClass().getResource("View/LoginStage.fxml"));

            // 设置stage拖动
            // 1 获取scene的偏移量
            pane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    xOffSet = event.getSceneX();
                    yOffSet = event.getSceneY();
                }
            });
            // 设置 stage 的x y
            pane.setOnMouseDragged(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    stage.setX(event.getScreenX() - xOffSet);
                    stage.setY(event.getScreenY() - yOffSet);
                }
            });

            stage.setScene(new Scene(pane));
            LoginStageController controller = fxmlLoader.getController();
            controller.setStage(stage);
            stage.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private void initMainStage() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("View/MainStage.fxml"));
        AnchorPane pane = loader.load();

    }
}
